package com.keqiang.chart.impl.radar

import android.graphics.Canvas
import androidx.annotation.FloatRange
import androidx.annotation.Px
import com.keqiang.chart.base.BaseChart
import com.keqiang.chart.base.BaseChartView
import com.keqiang.chart.impl.radar.render.IAxisRender
import com.keqiang.chart.impl.radar.render.IDataRender
import com.keqiang.chart.utils.px

/**
 * 雷达图基类
 *
 * @author Created by wanggaowan on 2022/10/27 10:38
 */
abstract class BaseRadarChart<D>(view: BaseChartView<*>) : BaseChart<D>(view) {
    /**
     * 饼状图半径
     */
    @setparam:Px
    @setparam:FloatRange(from = 0.0)
    open var radius: Float = 200f.px
        set(value) {
            if (value < 0) {
                return
            }
            field = value
        }

    /**
     * 起始绘制的角度
     */
    open var drawStartAngle: Float = 0f

    abstract val xAxisRender: IAxisRender

    abstract val yAxisRender: IAxisRender

    abstract val dataRender: IDataRender

    abstract override val touchHandler: BaseTouchHandler<out BaseRadarChart<D>, *>?

    override fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float) {
        if (radius < 0) {
            return
        }

        val (cx, cy) = getCenter()
        val startAngle = drawStartAngle + (touchHandler?.rotation ?: 0f)
        xAxisRender.onDraw(canvas, cx, cy, radius, startAngle)
        yAxisRender.onDraw(canvas, cx, cy, radius, startAngle)
        dataRender.onDraw(canvas, startAngle)
    }

    /**
     * 获取中心点坐标
     */
    open fun getCenter(): Pair<Float, Float> {
        val width = view.width - view.paddingStart - view.paddingEnd
        val height = view.height - view.paddingTop - view.paddingBottom
        val centerX = width / 2f + view.paddingStart
        val centerY = height / 2f + view.paddingTop
        return Pair(centerX, centerY)
    }
}
